package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.Category;
import com.powernode.domain.Prod;
import com.powernode.domain.ProdProp;
import com.powernode.service.CategoryService;
import com.powernode.service.ProdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RequestMapping("/prod/category")
@RestController
@Api(tags = "产品分类管理接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/table")
    @PreAuthorize("hasAuthority('prod:category:page')")
    @ApiOperation("产品分类分页")
    public ResponseEntity<List<Category>> getCategoryPage() {
        List<Category> list = categoryService.list();
        return ResponseEntity.ok(list);
    }


    @GetMapping("/listCategory")
    @PreAuthorize("hasAuthority('prod:category:page')")
    @ApiOperation("产品分类新增列表")
    public ResponseEntity<List<Category>> getlistCategory() {
        List<Category> list = categoryService.list();
        return ResponseEntity.ok(list);
    }


    @PostMapping
    @PreAuthorize("hasAuthority('prod:category:save')")
    @ApiOperation("新增分类")
    public ResponseEntity<Void> addCategory(@RequestBody Category category) {
        category.setRecTime(new Date());
        categoryService.save(category);
        return ResponseEntity.ok().build();
    }


    @GetMapping("/category/categoryInfo")
    @ApiOperation("小程序分类")
    public ResponseEntity<List<Category>> categoryInfo(@RequestParam(defaultValue = "0") Long parentId) {
        List<Category> categories = categoryService.list(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId, parentId));
        return ResponseEntity.ok(categories);
    }
}
